/**
 * Created by HaiHui on 2017/10/25.
 */
/**
 * Created by HaiHui on 2017/10/17.
 */
var table;
$(document).ready(function() {
    table = $('#information').DataTable({
        "pagingType": "simple_numbers", //设置分页控件的模式
        searching: false, //屏蔽datatales的查询框
        aLengthMenu: [10], //设置一页展示10条记录
        "bLengthChange": false, //屏蔽tables的一页展示多少条记录的下拉列表
        "ordering": false, //屏蔽排序
        "oLanguage": { //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "没有找到符合条件的数据",
            //  "sProcessing": "&lt;img src=’./loading.gif’ /&gt;",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "木有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"

            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true, //打开后台分页
        "ajax": {
            "url": "dust/deadInfoByGrave",
            "dataSrc": "aaData",
            "data": function(d) {
                d.deadStatus = "2";
            }
        },
        "columns": [{
            "data": "baseId",
            "visible": false
        },
            {
                "data":"dustId",
                "visible":false
            },
            {
                "data": "deadName",
                "title": "逝者姓名"
            },
            {
                "data": "deadCardNo",
                "title": "身份证号"
            },
            {
                "data": "deadTime",
                "title": "死亡时间"
            },
            {
                "data": "deadCremationDate",
                "title": "火化时间"
            },
            {
                "data":"familyName",
                "title":"亲属姓名"
            },
            {
                "data":"familyPhone",
                "title":"联系方式"
            },
            /*  {
                "data":"dustName",
                "title":"公墓名称"
            },*/
            {
                "data": null,
                "title": "操作"
            }
        ],
        "columnDefs": [{
            "targets": -1, //操作按钮目标列
            "data": null,
            "defaultContent": "<i title=\"详情\" class=\"dogo-js-search fa fa-search\"></i><i title=\"初葬\"  class=\"dogo-js-edit fa fa-user\"></i><i title=\"合葬\" class=\"dogo-js-photo fa fa-user-plus\"></i>"

        }],
    });
    //查看
    $('#information tbody').on('click', '.dogo-js-search', function() {
        var data = table.row($(this).parents('tr')).data();
        showDetail(data.baseId)
        // $('#search-modal').modal(data.prId);
    });
    //上传图片
    $('#information tbody').on('click', '.dogo-js-photo', function() {
        var data = table.row($(this).parents('tr')).data();
        //showDetail(data.dustId)

        window.location.href="secondGrave.html?baseId="+data.baseId+"&item_nav=setGrave";
        // $('#search-modal').modal(data.prId);
    });
    //del
    $('#information tbody').on('click', '.dogo-js-del', function() {
        var data = table.row($(this).parents('tr')).data();
        var confirmIdx = layer.confirm("确认要删除?", {
                btn: ['删除', '取消']
            },
            function() {
                var setting = {
                    url: "deadinfo/delete",
                    data: {
                        id: data.baseId
                    }
                }
                ajaxSubmit(setting, function(responseData) {
                    checkSubmitResult(responseData, {
                        error: "删除失败"
                    }, function(responseData) {
                        layer.alert(responseData.description);
                        $('#information').DataTable().ajax.reload();

                    });
                }, "删除失败");
            },
            function() {
                layer.close(confirmIdx);
            });

    });
    //edit
    $('#information tbody').on('click', '.dogo-js-edit', function() {

        var data = table.row($(this).parents('tr')).data();
        showEditModal(data.baseId,data.dustId);

    });
});

function search() {
    table.ajax.reload();

}



function showEditModal(dataId,dustId) {
    $("#hideBaseId").val(dataId);
    $("#hideDustId").val(dustId);
    $("#regionSelect").val("");
    $("#rowSelect").val("");
    $("#graveSelect").text("");
    $("#setGraveName").text("");


    var setting = {
        url: "dust/setGraveDetail",
        data: {
            id: dataId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取信息登记失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var deadInfo = responseData.data.deadInfo;
            var regionInfo = responseData.data.region;
            $("#first-modal").find("#infoDeadName1").text(deadInfo.deadName);
            $("#first-modal").find("#infoDeadSex1").text(deadInfo.dustSex);
            $("#first-modal").find("#infoDeadBirthday1").text(deadInfo.deadBirthday);
            $("#first-modal").find("#infoDeadCardNo1").text(deadInfo.deadCardNo);
            $("#first-modal").find("#infoDeadNation1").text(deadInfo.deadNation);
            $("#first-modal").find("#infoDeadGroup1").text(deadInfo.deadGroup);
            $("#first-modal").find("#infoDeadAddress1").text(deadInfo.deadAddress);
            $("#first-modal").find("#infoDeadTime1").text(deadInfo.deadTime);
            $("#first-modal").find("#infoDeadRemark1").text(deadInfo.deadRemark);
            $("#first-modal").find("#infoHopeBuryAddress1").text(deadInfo.hopeBuryAddress);
            $("#first-modal").find("#infoDustName1").text(deadInfo.dustName);
            $("#first-modal").find("#infoDeadCremationDate1").text(deadInfo.deadCremationDate);
            $("#first-modal").find("#infoFamilyName1").text(deadInfo.familyName);
            $("#first-modal").find("#infoFamilyRelateive1").text(deadInfo.familyRelateive);
            $("#first-modal").find("#infoFamilyPhone1").text(deadInfo.familyPhone);
            initSelect("regionSelect",regionInfo,"regionId","regionName",true,null);

            $("#first-modal").modal('show');
        });
    }, "获取信息登记失败");

}

function showDetail(dataId) {

    var setting = {
        url: "dust/setGraveDetail",

        data: {
            id: dataId
        }
    }
    ajaxSubmit(setting, function(responseData) {
        checkSubmitResult(responseData, {
            error: "获取信息登记失败"
        }, function(responseData) {
            //TODO 详情页面数据填充
            var deadInfo = responseData.data.deadInfo;
            var regionInfo = responseData.data.region;


            $("#search-modal").find("#infoDeadName").text(deadInfo.deadName);
            $("#search-modal").find("#infoDeadSex").text(deadInfo.dustSex);
            $("#search-modal").find("#infoDeadBirthday").text(deadInfo.deadBirthday);
            $("#search-modal").find("#infoDeadCardNo").text(deadInfo.deadCardNo);
            $("#search-modal").find("#infoDeadNation").text(deadInfo.deadNation);
            $("#search-modal").find("#infoDeadGroup").text(deadInfo.deadGroup);
            $("#search-modal").find("#infoDeadAddress").text(deadInfo.deadAddress);
            $("#search-modal").find("#infoDeadTime").text(deadInfo.deadTime);
            $("#search-modal").find("#infoDeadRemark").text(deadInfo.deadRemark);
            $("#search-modal").find("#infoHopeBuryAddress").text(deadInfo.hopeBuryAddress);
            $("#search-modal").find("#infoDustName").text(deadInfo.dustName);
            $("#search-modal").find("#infoDeadCremationDate").text(deadInfo.deadCremationDate);
            $("#search-modal").find("#infoFamilyName").text(deadInfo.familyName);
            $("#search-modal").find("#infoFamilyRelateive").text(deadInfo.familyRelateive);
            $("#search-modal").find("#infoFamilyPhone").text(deadInfo.familyPhone);
            //分配墓穴名称


            $("#search-modal").modal('show');
        });
    }, "获取信息登记失败");
}
$("#regionSelect").change(function() {
    //row刷新
    var regionId = $("#regionSelect").val();
    var setting = {
        url: "dust/selectRows",
        data: {
            id: regionId
        },
        dataType: 'json'

    }
    ajaxSubmit(setting, function (responseData) {
        checkSubmitResult(responseData, {
            error: "查询失败"
        }, function (responseData) {
            var data = responseData.data.rowList;

            initSelect("rowSelect", data, "rowId", "rowName", true, null);

        }, "查询失败");


    })
})
$("#rowSelect").change(function() {
    var rowId = $("#rowSelect").val();
    $.ajax({
        url: "dust/setGrave",
        data: {
            id: rowId
        },
        dataType: 'json',
        async: false,
        success: function(responseData) {
            if(responseData.result){
                var data = responseData.data;
                layer.open({
                    content: responseData.description+"--" + data.graveName + "号",
                    success: function(layero, index){
                        $("#graveSelect").text(data.graveName);
                        $("#hideGraveId").val(data.graveId);
                        $("#setGraveName").text($("#infoDustName1").text()+$("#regionSelect").find("option:selected").text()+"-"+ $("#rowSelect").find("option:selected").text()+"排-"+data.graveName+"号");
                    }
                });
            }else{
                layer.open({
                    content: responseData.description,
                    success: function(layero, index){
                        $("#graveSelect").text("");
                    }
                });
            }
        }
    });


})
function saveGraveInfo(){
    var dustId = $("#hideDustId").val();
    var baseId=$("#hideBaseId").val();
    var graveId=$("#hideGraveId").val();
    var graveName=$("#setGraveName").text();
    var confirmIdx = layer.confirm("确认要保存吗?", {
            btn: ['确定', '取消']
        },
        function() {
            var setting = {
                url: "dust/saveGraveInfo",
                data: {
                    baseId:baseId,
                    dustId:dustId,
                    graveId:graveId,
                    graveName:graveName,
                    type:"1"
                },
                dataType: 'json'

            }
            ajaxSubmit(setting, function(responseData) {
                checkSubmitResult(responseData, {
                    error: "删除失败"
                }, function(responseData) {
                    layer.alert(responseData.description);
                    $("#first-modal").modal('hide');

                });
            }, "删除失败");
        },
        function() {
            layer.close(confirmIdx);
        });


}
//添加、修改项目
$("#saveProjectInfo").click(function() {
    var opType = $(this).data("opType");
    var action = null;
    var formdata = {};
    //formdata = new FormData($("#editForm")[0]);

    formdata = $("#editForm").serializeJson()
    //formdata = $("#editForm").serializeArray();

    var errorMsg = null;

    if(opType == "add") {
        action = "deadinfo/add";
        errorMsg = "添加信息失败";
    } else if(opType == "edit") {
        action = "deadinfo/update";
        errorMsg = "修改信息失败";
    }

    if(action == null) {
        layer.alert("不支持的操作类型");
    } else {

        var setting = {
            url: action,
            data: JSON.stringify(formdata),
            cache: false,
            contentType: "application/json",
            processData: false,
            async: false

        }
        ajaxSubmit(setting, function(responseData) {
            checkSubmitResult(responseData, {
                error: errorMsg
            }, function(responseData) {
                $("#edit-modal").modal('hide');
                layer.alert(responseData.description);
                $('#information').DataTable().ajax.reload();

            });

        }, errorMsg);

    }
});